"""用于windows定时运行的程序"""
import json
import os
from covid_inquire import get_time
from auto_report import auto_report

CONFIG_PATH = "config.json"
INQUIRE_TIME = 10  # 查询核酸检测的次数


def run():
    # 读取填报参数
    with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
        config = json.load(f)
    # 查询核酸检测内容
    if config["auto_inquire"]:
        for i in range(INQUIRE_TIME):
            try:
                config["covid_test_time"] = get_time()
            except ValueError as e:
                print(f"核酸检测的第{i + 1}次查询，失败：{e.args[0]}")
            else:
                break
    # 未查询到
    if config["covid_test_time"] is None:
        config["covid_test"] = 0
        config["covid_result_index"] = 0
        print("未查找到48小时核酸检测结果")
    elif not isinstance(config["covid_test_time"], str) or len(config["covid_test_time"]) != len("2022-01-07 11:11"):
        print(f"核酸检测时间格式错误：“{config['covid_test_time']}“")
        return
    else:
        print(f"请检查核酸检测时间是否正确：“{config['covid_test_time']}“")
    # 填报
    try:
        auto_report(config["id"], config["password"], config["title"], config["temperature"], config["covid_test"],
                    config["covid_result_index"], config["covid_test_time"], config["isolation_index"],
                    config["code_color_index"])
    except ValueError as e:
        print(f"填报失败：{e.args[0]}")
    else:
        print("填报成功！建议再次运行本程序以检查是否提交成功。")


if __name__ == '__main__':
    run()
    os.system("pause")
